import {
	baseconfig
} from "../common/js/common.js"

const httpClient = {
	request: function(method, url, data) {

		var headers = {
			// "Content-Type": "application/x-www-form-urlencoded",
			"Authorization": `Bearer ${uni.getStorageSync("userInformation").token}`
		};

		return new Promise((resolve, reject) => {
			// uni.showLoading({
			// 	title: "加载中",
			// 	mask: true
			// })

			console.log(headers)

			uni.request({
				url: url,
				header: headers,
				data: data,
				method: method,
				success: function(res) {
					// uni.hideLoading()

					if (res.statusCode == 401) {
						uni.showToast({
							icon: 'none',
							duration: 1000,
							title: res.data.error.message
						})

						setTimeout(function() {
							uni.reLaunch({
								url: '../login/login'
							})
						}, 1500)
					} else {
						if (res.data.error != null) {
							uni.showToast({
								icon: 'none',
								duration: 1000,
								title: res.data.error.message
							})

							return
						} else {
							resolve(res.data)
						}
					}
				},
				fail: function(err) {
					uni.hideLoading()
					reject(err)
				}
			})
		})
	},
	Get: function(url, data) {
		let allurl = httpClient.geturl(url)
		return httpClient.request('GET', allurl, data)
	},
	Post: function(url, data) {
		let allurl = httpClient.geturl(url)
		return httpClient.request('POST', allurl, data)
	},
	geturl: function(url) {
		return baseconfig.server + url
	}
};

export {
	httpClient
}
